<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/9/26
 * Time: 14:06
 */
namespace app\admin\controller;

use app\admin\validate\AdminValidate;
use app\admin\validate\MobileValidate;
use app\service\IDBuilder;
use app\service\SMS;
use think\Cache;
use think\captcha\Captcha;
use app\admin\model\Admin as AdminModel;

class Login extends Common {


    /**后台注册页面
     * @return \think\response\View
     */
    public function register(){
        return view();
    }


    /**后台登录
     * @return \think\response\View
     */
    public function login(){
        return view();
    }


    /**
     * 后台用户注册
     */
    public function regAdmin(){
        (new AdminValidate())->goCheck();
        $smsCode = Cache::get('smsCode');
        $data = input('post.');
        $uid = IDBuilder::buildUserID();
        $name = 'blog'.substr($uid,1,6);
        $mobile = $data['mobile'];
        $password = md5(config('setting.salt').$data['password']);
        $getCode = $data['sms_code'];
        if($smsCode != $getCode){
            ajaxReturn(['code'=>401,'msg'=>'验证码不正确','data'=>[]]);
        }
        $admin = new AdminModel();
        $res = AdminModel::isRegister($mobile);
        if($res){
            ajaxReturn(['code'=>402,'msg'=>'手机号已经注册','data'=>[]]);
        }
        $res = AdminModel::registerAdmin($uid,$name,$mobile,$password);
        if($res){
            ajaxReturn(['code'=>200,'msg'=>'注册成功','data'=>[]]);
        }else{
            ajaxReturn(['code'=>400,'msg'=>'注册失败','data'=>[]]);
        }
    }


//    验证后台用户登录信息
    public function checkAdmin(){
        if(request()->isAjax()){
            $mobile = input('post.mobile');
            $password= input('post.password');
            $viewCode = input('post.viewCode');
            $password = md5(config('setting.salt').$password);
            if(!AdminModel::isRegister($mobile)){
                ajaxReturn(['code'=>401,'msg'=>'该手机号未注册','data'=>[]]);
            }
            $adminUser = AdminModel::checkPassword($mobile,$password);
            if(!$adminUser){
                ajaxReturn(['code'=>402,'msg'=>'密码错误，请重新输入','data'=>[]]);
            }
            $captcha = new Captcha();
            $result = $captcha->check($viewCode);
//           print_r($result);die;
            if(!$result){
                ajaxReturn(['code'=>403,'msg'=>'验证码不正确','data'=>[]]);
            }
            if($adminUser->status == 0){
                ajaxReturn(['code'=>404,'msg'=>'该用户已锁定','data'=>[]]);
            }
//            缓存用户登录信息
            $uid = $adminUser->u_id;
            $user_token = md5('salt'.$_SERVER['REQUEST_TIME'].$uid);
            $user_token_time = $_SERVER['REQUEST_TIME'];
            session('uid',$uid);
            session('user_token',$user_token);
            session('user_token_time',$user_token_time);
            //            获取用户最后登录 IP
            $last_login_ip = get_client_ip();
            AdminModel::where('u_id','=',$uid)->update(['login_ip'=>$last_login_ip]);
            ajaxReturn(['code'=>200,'msg'=>'登录成功','data'=>[]]);
        }
    }


    /**
     * 发送短信验证码
     */
    public function sendSms(){
        (new MobileValidate())->goCheck();
        $mobile = input('get.mobile');
        $sms = new SMS();
        $randNum = rand_string(6,1);
        $expire = 60;
        Cache::set('smsCode',$randNum,$expire);
        $res = true;
        $res = $sms->sendTemplateSMS($mobile,[$randNum,$expire/60],1);
        if($res){
            ajaxReturn(['code'=>200,'msg'=>'验证码发送成功','data'=>['expire'=>$expire]]);
        }
    }


    /**
     * 登录图形验证码
     * @return \think\Response
     */
    public function verify(){
        $config =    [
            'imageH'      => 35,
            'imageW'      => 115,
            // 验证码字体大小
            'fontSize'    => 18,
            // 验证码位数
            'length'      => 4,
            // 关闭验证码杂点
            'useNoise'    => false,
        ];
        $captcha = new Captcha($config);
        // 设置验证码字符为纯数字
        $captcha->codeSet = '0123456789';
        return $captcha->entry();
    }
}